package cn.xuchunh.exam;

import java.util.Scanner;

/**
 * 二分查找一个整数，找到输出index，找不到输出（-1 - 应该在的位置），输入为递增数组
 * Created by XuChunH on 2016/9/17.
 */
public class StrangeBinarySearch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int target  = scanner.nextInt();
        int length = scanner.nextInt();
        if(length == 0){
            System.out.println(-1);
            return;
        }
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            array[i] = scanner.nextInt();
        }
        System.out.println(findIndex(array, 0, length - 1, target));
    }

    private static int findIndex(int[] array, int start, int end, int target){
        if(end - start == 1){
            if(array[start] == target){
                return start;
            }else if(array[end] == target){
                return end;
            }else if(array[start] > target){
                return -1 - start;
            }else if(target < array[end] && start > array[start]){
                return  -1 - end;
            }else {
                return -2 - end;
            }
        }
        if(start >= end){
            if(array[start] == target){
                return start;
            }else {
                return -1 - start;
            }
        }
        int mid = (start + end) / 2;
        if(array[mid] == target){
            return mid;
        }else if(array[mid] > target){
            return findIndex(array, start, mid - 1, target);
        }else {
            return findIndex(array, mid + 1, end, target);
        }
    }
}
